﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Microsoft.Reporting.WebForms;
using MAPDavy.Entities;
using MAPDavy.BusinessLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace MAPDavy.WebMVC3
{
    public partial class ReportViewer : System.Web.UI.Page
    {
        [Dependency]
        public IReportBL ReportBL { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                container.BuildUp(this.GetType(), this);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var idReprot = Request.QueryString["report"];

                if (idReprot != null && Request.Cookies["workspace"] != null)
                {
                    var cookie = Request.Cookies["workspace"];

                    var reporte = ReportBL.GetFromReportById(Int32.Parse(idReprot.ToString()));

                    var reportServer = ConfigurationManager.AppSettings["ReportServer"].ToString();
                    var folderReport = ConfigurationManager.AppSettings["FolderReport"].ToString();

                    ReportViewerMAP.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
                    ReportViewerMAP.ServerReport.ReportServerUrl = new Uri(reportServer);
                    ReportViewerMAP.ServerReport.ReportPath = folderReport + reporte.FileNameReport;
                    ReportViewerMAP.ServerReport.Refresh();

                    List<ReportParameter> parametros = new List<ReportParameter>();
                    parametros.Add(new ReportParameter("AnioAnio", "[Anio].[Anio].&[" + cookie.Value.ToString() + "]"));

                    foreach (var item in reporte.ParameterReports)
                    {
                        parametros.Add(new ReportParameter(item.Name, item.Value));
                    }

                    ReportViewerMAP.ServerReport.SetParameters(parametros);
                    ReportViewerMAP.ServerReport.Refresh();
                }

            }
            else 
            {
                var param = ReportViewerMAP.ServerReport.GetParameters();  
            }
        }
    }
}